{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2bf735a6-1b46-4c14-900a-3a8fa8e56f71",
   "metadata": {},
   "source": [
    "# while  循环"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15788d2e-714c-4de9-b9b7-7c73ca0cca7e",
   "metadata": {},
   "source": [
    "![img](https://www.runoob.com/wp-content/uploads/2015/12/loop.png)while  循环"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fad533f-fae2-4869-8106-731ab2149267",
   "metadata": {},
   "source": [
    "``` python\n",
    "while 判断条件(condition)：    # 判断条件是布尔类型   TRUE False  或者  0  或 非零\n",
    "    执行语句(statements)……\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "19d01404-fdf8-4123-a159-fe855b1dc806",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "n = 100\n",
    "sums = 0\n",
    "counter = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3f2e9878-f64b-4fca-9afe-62b947b6dfde",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "while counter <= n:\n",
    "    sums = sums + counter\n",
    "    counter += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "54f63d0b-d5b1-47a7-8d0b-3b58d580cce9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 到 100 之和为: 5050\n"
     ]
    }
   ],
   "source": [
    "print(\"1 到 %d 之和为: %d\" % (n,sum))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "137287ad-d77f-428f-b4c5-9c2f471ea690",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "while counter <= n:\n",
    "    sums += counter\n",
    "    counter += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9a4a6e5a-d4e2-4e1a-85ae-59175d91e0b3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入一个数字  : 100\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你输入的数字是:  100\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入一个数字  : \n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: ''",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[19], line 3\u001b[0m\n\u001b[0;32m      1\u001b[0m var \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m var \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m : \u001b[38;5;66;03m# 表达式永远为 true   1 == 1 这个必然成立\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m     num \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m输入一个数字  :\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[0;32m      4\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m你输入的数字是: \u001b[39m\u001b[38;5;124m\"\u001b[39m, num) \n\u001b[0;32m      5\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mGood bye!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: ''"
     ]
    }
   ],
   "source": [
    "var = 1\n",
    "while var == 1 : # 表达式永远为 true   1 == 1 这个必然成立\n",
    "    num = int(input(\"输入一个数字  :\"))\n",
    "    print(\"你输入的数字是: \", num) \n",
    "print (\"Good bye!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "888ad51e-b570-494b-b708-519d0f27a99c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "a = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "eba8057f-d0b1-4acd-a557-b2360e59f15c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[31], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m100000000000000\u001b[39m):\n\u001b[1;32m----> 2\u001b[0m     a \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m i\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "for i in range(100000000000000):\n",
    "    print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f906e2f-7dbf-45f6-b8f5-e188fdb4477f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
